package com.vilen.weixin.kit;

import com.vilen.basic.model.User;
import com.vilen.basic.model.WeixinMenu;
import com.vilen.basic.model.WeixinQr;
import com.vilen.basic.service.IUserService;
import com.vilen.basic.service.IWeixinMenuService;
import com.vilen.basic.service.IWeixinQrService;
import com.vilen.weixin.model.WGroup;
import com.vilen.weixin.model.WUser;
import com.vilen.weixin.service.IWGroupService;
import com.vilen.weixin.service.IWUserService;
import com.vilen.weixin.web.servlet.BeanFactoryContext;

import java.io.IOException;
import java.util.Map;

/**
 * Created by Administrator on 2016/2/17.
 */
public class WeixinEventKit {
    public static String handlerMsg(Map<String, String> msgMap) throws IOException {
        String event = msgMap.get("Event");
        if("CLICK".equals(event)) {
            return handlerClickEvent(msgMap);
        } else if ("SCAN".equals(event)) {
            return handlerScanEvent(msgMap);
        } else if ("subscribe".equals(event)) {
            //用户关注事件
            return handlerSubscribeEvent(msgMap);
        } else if ("unsubscribe".equals(event)) {
            return handlerUnsubscribeEvent(msgMap);
        }
        return null;
    }

    private static String handlerScanEvent(Map<String, String> msgMap) throws IOException {
        //考虑一些问题 保存到数据库
        handlerUserInfo(msgMap);
        String snum = getSence(msgMap, false);
        String openid = msgMap.get("FromUserName");
        IWeixinQrService weixinQrService = (IWeixinQrService)BeanFactoryContext.getService("weixinQrService");
        WeixinQr wq = weixinQrService.loadBySnum(Integer.parseInt(snum));

        if (wq.getType() == WeixinQr.REPASSWORD_TYPE) {
            //处理修改密码操作
            return WeixinMessageKit.map2xml(MessageCreateKit.createTextMsg(msgMap,"<a href=\""+wq.getQrData()+"\">"+wq.getMsg() +"</a>"));
        } else if (wq.getType() == WeixinQr.SET_GROUP_TYPE) {
            //处理设置用户组的操作
            IWGroupService wGroupService = (IWGroupService) BeanFactoryContext.getService("wGroupService");
            wGroupService.moveUserToGroup(openid, Integer.parseInt(wq.getQrData()));
            WGroup wg = wGroupService.queryUserGroup(openid);
            return WeixinMessageKit.map2xml(MessageCreateKit.createTextMsg(msgMap,"你的用户组已经修改,用户组修改为"+wg.getName()));
        } else if (wq.getType() == WeixinQr.TEMP_BIND) {
            //处理绑定用户操作
            return WeixinMessageKit.map2xml(MessageCreateKit.createTextMsg(msgMap,"<a href=\""+wq.getQrData()+"\">"+wq.getMsg() +"</a>"));
        } else if (wq.getType() == WeixinQr.TEMP_LOGIN) {
            //处理用户扫码登录操作
            long t = System.currentTimeMillis()-wq.getCreateDate().getTime();
            if ((t / 1000) > 60) {
                return WeixinMessageKit.map2xml(MessageCreateKit.createTextMsg(msgMap, "你所扫描的二维码已经过期，请在网页中重新刷新并扫描！"));
            } else {
                //说明已经扫到
                wq.setStatus(1);
                wq.setQrData(openid);
                weixinQrService.update(wq);
            }
        }

        return null;
    }


    private static String handlerUnsubscribeEvent(Map<String, String> msgMap) {
        String openid = msgMap.get("FromUserName");
        IUserService userService = (IUserService) BeanFactoryContext.getService("userService");
        User u = userService.loadByOpenid(openid);
        if(u != null) {
            u.setStatus(0);
            userService.update(u);
        }
        return null;
    }

    private static String getSence(Map<String, String> msgMap, boolean subscribe) {
        String key = msgMap.get("EventKey");
        if (key == null || "".equals(key)) {
            return null;
        }
        if (subscribe) {
            return key.split("_")[1];
        }
        return key;
    }

    /**
     * 保存到数据库
     * @param msgMap
     * @return
     */
    private static User handlerUserInfo(Map<String, String> msgMap) {
        String openid = msgMap.get("FromUserName");
        IUserService userService = (IUserService) BeanFactoryContext.getService("userService");
        User u = userService.loadByOpenid(openid);
        if(u ==null) {
            IWUserService wUserService = (IWUserService) BeanFactoryContext.getService("wUserService");
            WUser wUser = wUserService.queryByOpenid(openid);
            u = wUser.getUser();
            userService.add(u);
        }else{
            if (u.getStatus() == 0) {
                u.setStatus(1);
                userService.update(u);
            }
        }
        return u;
    }

    private static String handlerSubscribeEvent(Map<String, String> msgMap) throws IOException {
        User u = handlerUserInfo(msgMap);
        String openid = msgMap.get("FromUserName");
        //未关注的时候
        String snum = getSence(msgMap, true);
        if (snum != null) {
            IWeixinQrService weixinQrService = (IWeixinQrService)BeanFactoryContext.getService("weixinQrService");
            WeixinQr wq = weixinQrService.loadBySnum(Integer.parseInt(snum));
            IWGroupService wGroupService = (IWGroupService) BeanFactoryContext.getService("wGroupService");
            if (wq.getType() == WeixinQr.SET_GROUP_TYPE) {
                wGroupService.moveUserToGroup(openid,Integer.parseInt(wq.getQrData()));
            }

        }


        if(u.getBind()==0) {
            //没有绑定
            String bindUrl = "http://duowuggikm.proxy.qqbrowser.cc/wx/user/bindExistUser";
            return WeixinMessageKit.map2xml(MessageCreateKit.createTextMsg(msgMap,"<a href=\""+bindUrl+"\">请绑定用户获得更好的体验</a>"));
        } else{
            String bindUrl = "http://www.konghao.org";
            return WeixinMessageKit.map2xml(MessageCreateKit.createTextMsg(msgMap,"<a href=\""+bindUrl+"\">欢迎您再次使用我们的微信平台，点击打开我们的页面</a>"));
        }
    }

    private static String handlerClickEvent(Map<String, String> msgMap) throws IOException {
        String keyCode = msgMap.get("EventKey");
        IWeixinMenuService weixinMenuService = (IWeixinMenuService) BeanFactoryContext.getService("weixinMenuService");
        WeixinMenu wm = weixinMenuService.loadByKey(keyCode);
        if(wm!=null&&wm.getResponType()==1) {
            Map<String,Object> map = MessageCreateKit.createTextMsg(msgMap,wm.getContent());
            return WeixinMessageKit.map2xml(map);
        }
        return null;
    }
}
